<?php

if(isset($_POST["change_group"]))
{
    if($Users->change_group($_POST["group"],$_POST["backup"],$user))
        header("Location: /?page=registrations");
}

if(isset($_POST["cancel_registration"]))
{
    if($Users->cancel_registration($_POST["backup"],$user))
        header("Location: /?page=registrations");
}

?>
<div id="content">
    <h2>Ilmoittautumiseni</h2>
    <ul>
        <? $registrations = $Users->get_registrations($user);
        foreach($registrations as &$registration)
        {
            if($registration->teacher != null)
                $teacher = " (" . e($registration->teacher) . ")";
            else
                $teacher = null;
            if($registration->superviser != null)
                $superviser = " (" . e($registration->superviser) . ")";
            else
                $superviser = null;
            ?>
            <li>
                <h3><a href="javascript:void(0);" onclick="toggle(this)" class="toggle"><? echo e($registration->course); ?></a></h3>
                <ul style="display:none;">
                    <li>
                        <h4>Luennot<? echo $teacher; ?></h4>
                        <table class="timetable">
                            <thead>
                                <tr>
                                    <th class="number">#</th>
                                    <th class="date">Päivä</th>
                                    <th class="time">Aika</th>
                                    <th class="location">Paikka</th>
                                </tr>
                            </thead>
                            <tbody>
                                <? $i = 0; $lectures = $Lectures->get_by_instance($registration->instance);
                                foreach($lectures as &$lecture) { ++$i; ?>
                                    <tr>
                                        <td class="number"><? echo $i; ?></td>
                                        <td class="date"><? echo date("d.m.Y",$lecture->date); ?></td>
                                        <td class="time"><? echo date("H:i",$lecture->start_time); ?>—<? echo date("H:i",$lecture->end_time); ?></td>
                                        <td class="location"><? echo e($lecture->location); ?></td>
                                    </tr>
                                <? } ?>
                            </tbody>
                        </table>
                    </li>
                    <li>
                        <h4>Kokeet</h4>
                        <table class="timetable">
                            <thead>
                                <tr>
                                    <th class="number">#</th>
                                    <th class="date">Päivä</th>
                                    <th class="time">Aika</th>
                                    <th class="location">Paikka</th>
                                </tr>
                            </thead>
                            <tbody>
                                <? $i = 0; $exams = $Exams->get_by_instance($registration->instance);
                                foreach($exams as &$exam) { ++$i; ?>
                                    <tr>
                                        <td class="number"><? echo $i; ?></td>
                                        <td class="date"><? echo date("d.m.Y",$exam->date); ?></td>
                                        <td class="time"><? echo date("H:i",$exam->start_time); ?>—<? echo date("H:i",$exam->end_time); ?></td>
                                        <td class="location"><? echo e($exam->location); ?></td>
                                    </tr>
                                <? } ?>
                            </tbody>
                        </table>
                    </li>
                    <li>
                        <h4>Harjoituskerrat<? echo $superviser; ?></h4>
                        <table class="timetable">
                            <thead>
                                <tr>
                                    <th class="number">#</th>
                                    <th class="date">Päivä</th>
                                    <th class="time">Aika</th>
                                    <th class="location">Paikka</th>
                                </tr>
                            </thead>
                            <tbody>
                                <? $i = 0; $meetings = $Meetings->get_by_group($registration->group);
                                foreach($meetings as &$meeting) { ++$i; ?>
                                    <tr>
                                        <td class="number"><? echo $i; ?></td>
                                        <td class="date"><? echo date("d.m.Y",$meeting->date); ?></td>
                                        <td class="time"><? echo date("H:i",$meeting->start_time); ?>—<? echo date("H:i",$meeting->end_time); ?></td>
                                        <td class="location"><? echo e($meeting->location); ?></td>
                                    </tr>
                                <? } ?>
                            </tbody>
                        </table>
                    </li>
                    <li>
                        <h4><a href="javascript:void(0);" onclick="toggle(this)" class="toggle">Muokkaa ilmoittautumista</a></h4>
                        <form method="post" action="?page=registrations" style="display:none;">
                            <ol>
                                <? $groups =  $Groups->get_by_instance($registration->instance);
                                foreach($groups as &$group)
                                {
                                    if($registration->group == $group->id)
                                        $checked = "checked='checked'";
                                    else
                                        $checked = null;
                                    ?>
                                    <li>
                                        <h5><input type="radio" name="group" value="<? echo e($group->id); ?>" <? echo $checked; ?>><? echo e($group->superviser_name); ?></input></h5>
                                        <table class="timetable">
                                            <thead>
                                                <tr>
                                                    <th class="number">#</th>
                                                    <th class="date">Päivä</th>
                                                    <th class="time">Aika</th>
                                                    <th class="location">Paikka</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <? $i = 0; $meetings = $Meetings->get_by_group($group->id);
                                                foreach($meetings as &$meeting) { ++$i; ?>
                                                    <tr>
                                                        <td class="number"><? echo $i; ?></td>
                                                        <td class="date"><? echo date("d.m.Y",$meeting->date); ?></td>
                                                        <td class="time"><? echo date("H:i",$meeting->start_time); ?>—<? echo date("H:i",$meeting->end_time); ?></td>
                                                        <td class="location"><? echo e($meeting->location); ?></td>
                                                    </tr>
                                                <? } ?>
                                            </tbody>
                                        </table>
                                    </li>
                                <? } ?>
                            </ol>
                            <input type="hidden" name="backup" value="<? echo e($registration->group); ?>" />
                            <input type="submit" name="change_group" value="Vaihda ryhmää" />
                            <input type="submit" name="cancel_registration" value="Peru ilmoittautuminen" />
                        </form>
                    </li>
                </ul>
            </li>
        <? } ?>
    </ul>
</div>
